package com.movitech.mobile.cz.base.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.IdGenUtils;
import com.movitech.mobile.cz.base.common.util.ListObjCopyUtil;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.common.utils.JwtUtils;
import com.movitech.mobile.cz.base.modules.config.MessagesMsgTypeEnum;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.modules.vo.PageRequestVO;
import com.movitech.mobile.cz.base.purchase.bizvo.MyDealsResponseVO;
import com.movitech.mobile.cz.base.supplier.entity.SMyDeals;
import com.movitech.mobile.cz.base.sys.bizvo.NoticeResponseVO;
import com.movitech.mobile.cz.base.sys.entity.SMessages;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSMessages;
import com.movitech.mobile.cz.base.sys.entity.ext.SMessagesReqVO;
import com.movitech.mobile.cz.base.sys.mapper.SMessagesMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSMessageTemplateMapper;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSMessagesMapper;
import com.movitech.mobile.cz.base.sys.service.SMessagesService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 消息通知 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-01
 */
@Service("sMessagesService")
@Transactional
public class SMessagesServiceImpl extends ServiceImpl<SMessagesMapper, SMessages> implements SMessagesService {

	@Autowired
	private ExtSMessagesMapper extSMessagesMapper;

	@Override
	public boolean addSMessages(SMessages sMessages) {

		LicenseUtil.INSTANCE.validate();
		if(StringUtils.isBlank(sMessages.getId())){
			sMessages.setId(IdGenUtils.uuid());
		}

		Date createDate = new Date();
		String userId = UserUtils.getCurrentUserId();
		sMessages.setCreateBy(userId);
		sMessages.setCreateDate(createDate);
		sMessages.setUpdateBy(userId);
		sMessages.setUpdateDate(createDate);
		sMessages.setDelFlag(CommonConstants.NO_INTEGER);

		int i = baseMapper.insert(sMessages);

		return i > 0;
	}

	/**
	 * 采购管理后台首页公告通知获取(除外网通知)
	 */
	@Override
	public List<ExtSMessages> getNoticeListByShowNum(Map<String, Object> map) {
		return extSMessagesMapper.getNoticeListByShowNum(map);
	}

	@Override
	public RestReturnResult<Object> allMessageList(HttpServletRequest request, HttpServletResponse response, SMessagesReqVO sMessagesReqVO) {
		RestReturnResult<Object> hResult = RestReturnResult.error();
		LicenseUtil.INSTANCE.validate();
		try {
			String userId=UserUtils.getOuterCurrentUserId();
			if(StringUtils.isEmpty(userId)){
				return RestReturnResult.success();
			}
			if(sMessagesReqVO.getPageFlag()!=null&&sMessagesReqVO.getPageFlag().equals(CommonConstants.YES_INTEGER)){
				Page<SMessages> page=new Page(sMessagesReqVO.getPageNumber(), sMessagesReqVO.getPageSize(), " update_date ", false);
				EntityWrapper wrapper = new EntityWrapper<>();
				SMessages sMessages=new SMessages();
				sMessages.setUserIds(userId);
				sMessages.setMsgType(MessagesMsgTypeEnum.ANNOUNCEMENT_NOTICE.getCode());
				sMessages.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
				if(sMessagesReqVO.getReadFlag()!=null)sMessages.setReadFlag(sMessagesReqVO.getReadFlag());
				wrapper.setEntity(sMessages);
				wrapper.orderBy(" update_date ",false );
				Page<SMessages> sMyDealsPage= this.selectPage(page,wrapper);
				Page<NoticeResponseVO> NoticeResponseVOPage= new Page<NoticeResponseVO>();
				List<NoticeResponseVO> myDealsResponseVOList = ListObjCopyUtil.copyTo(sMyDealsPage.getRecords(), NoticeResponseVO.class);
				NoticeResponseVOPage.setRecords(myDealsResponseVOList);
				NoticeResponseVOPage.setTotal(sMyDealsPage.getTotal());
				NoticeResponseVOPage.setSize(sMyDealsPage.getSize());
				NoticeResponseVOPage.setCurrent(sMyDealsPage.getCurrent());
				hResult = RestReturnResult.success();
				hResult.setData(NoticeResponseVOPage);
			}else{
				EntityWrapper wrapper = new EntityWrapper<>();
				SMessages sMessages=new SMessages();
				sMessages.setUserIds(userId);
				sMessages.setMsgType(MessagesMsgTypeEnum.ANNOUNCEMENT_NOTICE.getCode());
				sMessages.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
				if(sMessagesReqVO.getReadFlag()!=null)sMessages.setReadFlag(sMessagesReqVO.getReadFlag());
				wrapper.setEntity(sMessages);
				wrapper.orderBy(" update_date ",false );
				List<SMessages> ls=this.selectList(wrapper);
				List<NoticeResponseVO> NoticeResponseVOList = ListObjCopyUtil.copyTo(ls, NoticeResponseVO.class);
				hResult = RestReturnResult.success();
				hResult.setData(NoticeResponseVOList);
			}
		}catch (Exception e){
			e.printStackTrace();
			hResult = RestReturnResult.error();
		}
		return hResult;
	}

	@Override
	public RestReturnResult<Object> readSMessage(HttpServletRequest request, HttpServletResponse response, String messageId) {
		RestReturnResult<Object> hResult = RestReturnResult.error();
		LicenseUtil.INSTANCE.validate();
		SMessages sMessages = this.selectById(messageId);
		if(sMessages==null){
			hResult.setMsg("消息不存在");
			return hResult;
		}
		if(sMessages.getReadFlag()!=null&&CommonConstants.NO_INTEGER==sMessages.getReadFlag()){
			sMessages.setReadFlag(CommonConstants.YES_INTEGER);
			sMessages.setUpdateDate(new Date());
			this.updateById(sMessages);
		}
		return RestReturnResult.success();
	}

	@Override
	public int getMessageCounts(String userId, List<Integer> msgTypeList, boolean includeFlag) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userId", userId);
		param.put("msgTypeList", msgTypeList);
		param.put("includeFlag", includeFlag);

		return extSMessagesMapper.getMessageCounts(param);
	}

	@Override
	public long getMessageListByParamsCounts(Map<String, Object> map) {
		return extSMessagesMapper.getMessagesListCount(map);
	}

	@Override
	public List<ExtSMessages> getMessageListByParams(Map<String, Object> map) {
		return extSMessagesMapper.getMessagesList(map);
	}

	@Override
	public long getNoticeListCount(Map<String, Object> map) {
		return extSMessagesMapper.getNoticeListCount(map);
	}

}
